<template>
  <el-dialog
    :title="!dataForm.bargainId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品主键" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品主键"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品标题" prop="goodsTitle">
      <el-input v-model="dataForm.goodsTitle" placeholder="商品标题"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endDate">
      <el-input v-model="dataForm.endDate" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="低价" prop="floorPrice">
      <el-input v-model="dataForm.floorPrice" placeholder="低价"></el-input>
    </el-form-item>
    <el-form-item label="需要砍多少刀" prop="knifeCount">
      <el-input v-model="dataForm.knifeCount" placeholder="需要砍多少刀"></el-input>
    </el-form-item>
    <el-form-item label="每刀最多砍多少" prop="knifePrice">
      <el-input v-model="dataForm.knifePrice" placeholder="每刀最多砍多少"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="bargainDescribe">
      <el-input v-model="dataForm.bargainDescribe" placeholder="描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bargainId: 0,
          goodsId: '',
          goodsName: '',
          goodsTitle: '',
          goodsPrice: '',
          createDate: '',
          endDate: '',
          floorPrice: '',
          knifeCount: '',
          knifePrice: '',
          bargainDescribe: ''
        },
        dataRule: {
          goodsId: [
            { required: true, message: '商品主键不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsTitle: [
            { required: true, message: '商品标题不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          floorPrice: [
            { required: true, message: '低价不能为空', trigger: 'blur' }
          ],
          knifeCount: [
            { required: true, message: '需要砍多少刀不能为空', trigger: 'blur' }
          ],
          knifePrice: [
            { required: true, message: '每刀最多砍多少不能为空', trigger: 'blur' }
          ],
          bargainDescribe: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bargainId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bargainId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/info/${this.dataForm.bargainId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.bargain.goodsId
                this.dataForm.goodsName = data.bargain.goodsName
                this.dataForm.goodsTitle = data.bargain.goodsTitle
                this.dataForm.goodsPrice = data.bargain.goodsPrice
                this.dataForm.createDate = data.bargain.createDate
                this.dataForm.endDate = data.bargain.endDate
                this.dataForm.floorPrice = data.bargain.floorPrice
                this.dataForm.knifeCount = data.bargain.knifeCount
                this.dataForm.knifePrice = data.bargain.knifePrice
                this.dataForm.bargainDescribe = data.bargain.bargainDescribe
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/${!this.dataForm.bargainId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bargainId': this.dataForm.bargainId || undefined,
                'goodsId': this.dataForm.goodsId,
                'goodsName': this.dataForm.goodsName,
                'goodsTitle': this.dataForm.goodsTitle,
                'goodsPrice': this.dataForm.goodsPrice,
                'createDate': this.dataForm.createDate,
                'endDate': this.dataForm.endDate,
                'floorPrice': this.dataForm.floorPrice,
                'knifeCount': this.dataForm.knifeCount,
                'knifePrice': this.dataForm.knifePrice,
                'bargainDescribe': this.dataForm.bargainDescribe
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
